﻿ using Developer.Code;
using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Secure;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System.Linq;
using System.Web.Mvc;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class MainController : BaseController
    {
       
        public ActionResult Index()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = AdminerService.GetAll().Where(a => a.IsAbort == false && a.Identity < 0);
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.Identity)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new Adminer
                 {
                     Id = a.Id,
                     Account = a.Account,
                     Identity = a.Identity,
                     OpenId = a.OpenId,
                     Password = a.Password,
                     Phone = a.Phone,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }


        public ActionResult EditAdmin()
        {
            var typeId = Query.Value("typeId").ToInt();
            var CycleType = new Adminer();
            if (typeId > 0)
            {
                CycleType = AdminerService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(CycleType);
        }

        [HttpPost]
        public ActionResult EditAdmin(int? z)
        {
            var typename = Query.Value("typename");
            var name = Query.Value("name");
            var phone = Query.Value("phone");
            var pwd = Query.Value("pwd").Md5String();
            var account = Query.Value("account");
            var typeId = Query.Value("typeId").ToInt();
            var CycleType = new Adminer();
            if (typeId > 0)
            {
                CycleType = AdminerService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (CycleType != null)
                {
                    CycleType.Name = name;
                    CycleType.Password = pwd;
                    CycleType.Phone = phone;
                    CycleType.Account = account;
                    AdminerService.Change(CycleType);
                }
            }
            else
            {
                CycleType.Name = name;
                CycleType.Password = pwd;
                CycleType.Phone = phone;
                CycleType.Identity = -1;
                CycleType.Account = account;
                CycleType.IsAbort = false;
                AdminerService.Add(CycleType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/Cycle/edittype?typeId=" + typeId);
        }

        public ActionResult DeleteAdmin()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                AdminerService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/Cycle/index");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/Cycle/edittype");
        }

#region 调整顺序

        /// <summary>
        /// 调整各项在数据库中的顺序
        /// </summary>
        /// <returns></returns>
        public ActionResult EditRepairModelSequence(int? bid)
        {
            if (!bid.HasValue)
            {
                bid = RepairBrandService.GetAll().FirstOrDefault().Id;
            }
            ViewBag.bid = bid;
            var repairModel = RepairModelService.GetAll().Where(a => a.IsAbort == false && a.RepairBrandId == bid);
            return View(repairModel);
        }
        public ActionResult EditRepairBrandSequence()
        {
            var repairBrand = RepairBrandService.GetAll().Where(a => a.IsAbort == false);
            return View(repairBrand);
        }
        public ActionResult EditCycleModelSequence(int? bid)
        {
            if (!bid.HasValue)
            {
                bid = CycleBrandService.GetAll().FirstOrDefault().Id;
            }
            ViewBag.bid = bid;
            var cycleModel = CycleModelService.GetAll().Where(a => a.IsAbort == false && a.CycleBrandId == bid);
            return View(cycleModel);
        }
        public ActionResult EditCycleBrandSequence()
        {
            var cycleBrand = CycleBrandService.GetAll().Where(a => a.IsAbort == false);
            return View(cycleBrand);
        }

        /// <summary>
        /// 保存顺序 AJAX  
        /// type : 1 => repairBrand,  2=>repairModel,  3=>cycleBrand,  4=>cycleModel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveSequence()
        {
            var type = Query.Value("type").ToInt();
            var depIds = Query.Value("id").Split(',');
            var orders = Query.Value("order").Split(',');
            switch (type) {
                case 1: 
                    for(int i = 0; i< depIds.Length; i++)
                    {
                        for(int j = 0; j< orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var repairBrand = RepairBrandService.GetAll().Find(depIds[i]);
                                repairBrand.Sequence = orders[j].ToInt();
                                RepairBrandService.Change(repairBrand);
                            }
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < depIds.Length; i++)
                    {
                        for (int j = 0; j < orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var repairModel = RepairModelService.GetAll().Find(depIds[i].ToInt());
                                repairModel.Sequence = orders[j].ToInt();
                                RepairModelService.Change(repairModel);
                            }
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < depIds.Length; i++)
                    {
                        for (int j = 0; j < orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var cycleBrand = CycleBrandService.GetAll().Find(depIds[i].ToInt());
                                cycleBrand.Sequence = orders[j].ToInt();
                                CycleBrandService.Change(cycleBrand);
                            }
                        }
                    }
                    break;
                case 4:
                    for (int i = 0; i < depIds.Length; i++)
                    {
                        for (int j = 0; j < orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var cycleModel = CycleModelService.GetAll().Find(depIds[i]);
                                cycleModel.Sequence = orders[j].ToInt();
                                CycleModelService.Change(cycleModel);
                            }
                        }
                    }
                    break;
            }
            return Json(new { success = "Success" }, JsonRequestBehavior.AllowGet);
        }




        public ActionResult EditRepairProblemDetailSequence(int? bid)
        {
            if (!bid.HasValue)
            {
                bid = RepairProblemService.GetAll().FirstOrDefault().Id;
            }
            ViewBag.bid = bid;
            var repairModel = RepairProblemDetailService.GetAll().Where(a => a.IsAbort == false && a.RepairProblemId == bid);
            return View(repairModel);
        }
        public ActionResult EditRepairProblemSequence(int? bid)
        {
            if (!bid.HasValue)
            {
                bid = RepairColorService.GetAll().FirstOrDefault().Id;
            }
            ViewBag.bid = bid;
            var repairBrand = RepairProblemService.GetAll().Where(a => a.IsAbort == false && a.RepairColorId == bid);
            return View(repairBrand);
        }
        public ActionResult EditCycleProblemDetailSequence(int? bid)
        {
            if (!bid.HasValue)
            {
                bid = CycleProblemService.GetAll().FirstOrDefault().Id;
            }
            ViewBag.bid = bid;
            var cycleModel = CycleProblemDetailService.GetAll().Where(a => a.IsAbort == false && a.CycleProblemId == bid);
            return View(cycleModel);
        }
        public ActionResult EditCycleProblemSequence(int? bid)
        {
            if (!bid.HasValue)
            {
                bid = CycleModelService.GetAll().FirstOrDefault().Id;
            }
            ViewBag.bid = bid;
            var cycleBrand = CycleProblemService.GetAll().Where(a => a.IsAbort == false && a.CycleModelId == bid);
            return View(cycleBrand);
        }

        /// <summary>
        /// 保存顺序 AJAX  
        /// type : 1 => repairProblem,  2=>repairProblemDetail,  3=>cycleProblem,  4=>cycleProblemDetail
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveSequence2()
        {
            var type = Query.Value("type").ToInt();
            var depIds = Query.Value("id").Split(',');
            var orders = Query.Value("order").Split(',');
            switch (type)
            {
                case 1:
                    for (int i = 0; i < depIds.Length; i++)
                    {
                        for (int j = 0; j < orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var repairBrand = RepairProblemService.GetAll().Find(depIds[i]);
                                repairBrand.Sequence = orders[j].ToInt();
                                RepairProblemService.Change(repairBrand);
                            }
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < depIds.Length; i++)
                    {
                        for (int j = 0; j < orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var repairModel = RepairProblemDetailService.GetAll().Find(depIds[i].ToInt());
                                repairModel.Sequence = orders[j].ToInt();
                                RepairProblemDetailService.Change(repairModel);
                            }
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < depIds.Length; i++)
                    {
                        for (int j = 0; j < orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var cycleBrand = CycleProblemService.GetAll().Find(depIds[i].ToInt());
                                cycleBrand.Sequence = orders[j].ToInt();
                                CycleProblemService.Change(cycleBrand);
                            }
                        }
                    }
                    break;
                case 4:
                    for (int i = 0; i < depIds.Length; i++)
                    {
                        for (int j = 0; j < orders.Length; j++)
                        {
                            if (i == j)
                            {
                                var cycleModel = CycleProblemDetailService.GetAll().Find(depIds[i]);
                                cycleModel.Sequence = orders[j].ToInt();
                                CycleProblemDetailService.Change(cycleModel);
                            }
                        }
                    }
                    break;
            }
            return Json(new { success = "Success" }, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}